﻿using System;
using System.IO;
using System.Threading;
using System.Windows;
using WPFControls;

namespace PTExpress
{
    public class Utility
    {
        #region Image

        public static byte[] ImageToByteArray(string imagePath)
        {
            byte[] arr = null;
            try
            {
                FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                arr = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Utility.ShowError(ex.Message, "ImageToByteArray Error");
            }
            return arr;
        }

        public static void DeleteErrorImages()
        {
            // Delete a directory and all subdirectories with Directory static method...
            string path = AppDomain.CurrentDomain.BaseDirectory + "Resources\\ErrorImages\\";
            if (System.IO.Directory.Exists(path))
            {
                try
                {
                    System.IO.Directory.Delete(path, true);
                }
                catch { }
            }
        }

        #endregion // Image ----------------------------------------------------------------------------

        #region ShowMessage

        #region Without Owner

        public static void ShowExcuteSuccess(string msg, string title)
        {
            WPFMessageBox.Show(msg, title, WPFMessageBoxImage.OK);
        }

        public static void ShowError(string msg, string title)
        {
            WPFMessageBox.Show(msg, title, WPFMessageBoxImage.Error);
        }

        public static void ShowQueryError(string msg, string title)
        {
            WPFMessageBox.Show("Lỗi thao tác csdl. " + msg, title, WPFMessageBoxImage.Error);
        }

        public static void ShowConnectionError(string title)
        {
            WPFMessageBox.Show("Không thể kết nối đến server.", title, WPFMessageBoxImage.Error);
        }

        public static WPFMessageBoxResult ShowYesNoMessageBox(string msg, string title)
        {
            return WPFMessageBox.Show(msg, title, WPFMessageBoxButtons.YesNo, WPFMessageBoxImage.Question);
        }
        #endregion // Without Owner ----------------------------------------------------------

        #region With Owner

        public static void ShowExcuteSuccess(string msg, string title, Window owner)
        {
            WPFMessageBox.Show(msg, title, WPFMessageBoxButtons.OK, WPFMessageBoxImage.OK, owner);
        }

        public static void ShowError(string msg, string title, Window owner)
        {
            WPFMessageBox.Show(msg, title, WPFMessageBoxButtons.OK, WPFMessageBoxImage.Error, owner);
        }

        public static void ShowQueryError(string msg, string title, Window owner)
        {
            WPFMessageBox.Show("Lỗi thao tác csdl. " + msg, title, WPFMessageBoxButtons.OK,
                WPFMessageBoxImage.Error, owner);
        }

        public static void ShowConnectionError(string title, Window owner)
        {
            WPFMessageBox.Show("Không thể kết nối đến server.", title, WPFMessageBoxButtons.OK,
                WPFMessageBoxImage.Error, owner);
        }

        #endregion // With Owner -------------------------------------------------------------

        #endregion // ShowMessage ------------------------------------------------------------------------

        #region ProgressBar

        static Views.ProgressBar pgb;
        static Thread pgbThread;

        public static void Init()
        {
            pgbThread = new Thread(new ThreadStart(ThreadStart));
            pgbThread.SetApartmentState(ApartmentState.STA);
            pgbThread.IsBackground = true;
            pgbThread.Start();
            Thread.Sleep(100);
        }

        static void ThreadStart()
        {
            pgb = new Views.ProgressBar();
            pgb.Topmost = true;
            System.Windows.Threading.Dispatcher.Run();
        }

        public static void ShowProgressBar()
        {
            pgb.Dispatcher.BeginInvoke((Action)(() => { pgb.Active(); }));
        }

        public static void HideProgressBar()
        {
            pgb.Dispatcher.BeginInvoke((Action)(() => { pgb.DeActive(); GC.Collect(); }));
        }

        #endregion // ProgressBar ----------------------------------------------------------------------------

        public static void WriteError(string message)
        {
            string path = @"C:\Users\Administrator\AppData\Roaming\QLNB\QLNBErrorLog\ErrorLog\";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path += DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".txt";

            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("------------" + DateTime.Now.ToLongTimeString() + "------------");
                    sw.WriteLine(message);
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine("------------" + DateTime.Now.ToLongTimeString() + "------------");
                    sw.WriteLine(message);
                }
            }
        }

    }
}
